import Mock from 'mockjs'

// Mock 配置
Mock.setup({
  timeout: '200-600'
})

// 工具函数：格式化日期
function formatDate(date) {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 模拟已报名赛事数据
const enrolledCompetition = {
  id: 1,
  name: '蓝桥杯Web组竞赛',
  time: '2024-03-15',
  location: '黄冈',
  status: 1, // 0: 未开始, 1: 进行中, 2: 已结束
  // 赛事相关的知识点体系
  knowledgePoints: [
    {
      id: 1,
      title: 'HTML',
      mastered: false,
      children: [
        { id: 11, title: 'HTML5新特性', mastered: false },
        { id: 12, title: '语义化标签', mastered: false },
        { id: 13, title: 'Web存储', mastered: false },
        { id: 14, title: '多媒体处理', mastered: false }
      ]
    },
    {
      id: 2,
      title: 'CSS',
      mastered: false,
      children: [
        { id: 21, title: 'CSS3新特性', mastered: false },
        { id: 22, title: 'Flex布局', mastered: true },
        { id: 23, title: '网格布局', mastered: false },
        { id: 24, title: '响应式设计', mastered: false }
      ]
    },
    {
      id: 3,
      title: 'JavaScript',
      mastered: false,
      children: [
        { id: 31, title: 'ES6+特性', mastered: true },
        { id: 32, title: '异步编程', mastered: false },
        { id: 33, title: 'DOM操作', mastered: true },
        { id: 34, title: '事件处理', mastered: false }
      ]
    },
    {
      id: 4,
      title: 'Vue.js',
      mastered: false,
      children: [
        { id: 41, title: '组件开发', mastered: true },
        { id: 42, title: '状态管理', mastered: false },
        { id: 43, title: '路由系统', mastered: false },
        { id: 44, title: '生命周期', mastered: true }
      ]
    },
    {
      id: 5,
      title: '网络通信',
      mastered: false,
      children: [
        { id: 51, title: 'Axios使用', mastered: true },
        { id: 52, title: 'RESTful API', mastered: false },
        { id: 53, title: '接口调试', mastered: false }
      ]
    }
  ]
}

// 生成最近7天的打卡记录（不包括今天）
const generateRecentCheckIns = () => {
  const records = {}
  const today = new Date()
  
  // 生成昨天及之前6天的打卡记录
  for (let i = 1; i < 8; i++) {
    const date = new Date()
    date.setDate(today.getDate() - i)
    records[formatDate(date)] = Math.random() > 0.3 // 70% 概率已打卡
  }
  
  return records
}

// 模拟打卡数据
const checkInData = {
  records: generateRecentCheckIns(),
  streak: 2,
  todayChecked: false // 添加今天是否打卡的标记
}

// Mock API 响应
Mock.mock(/\/learning\/enrolled-competition/, 'get', () => {
  return {
    code: 200,
    data: enrolledCompetition,
    message: 'success'
  }
})

Mock.mock(/\/learning\/check-in\/records/, 'get', () => {
  return {
    code: 200,
    data: checkInData,
    message: 'success'
  }
})

Mock.mock(/\/learning\/check-in/, 'post', () => {
  const today = formatDate(new Date())
  checkInData.records[today] = true
  checkInData.todayChecked = true // 更新今天的打卡状态
  checkInData.streak += 1
  
  return {
    code: 200,
    data: checkInData,
    message: 'success'
  }
})

Mock.mock(/\/learning\/knowledge-points\/update/, 'post', (options) => {
  const { parentId, id, mastered } = JSON.parse(options.body)
  
  // 更新知识点状态
  if (id) {
    // 更新子知识点
    const parent = enrolledCompetition.knowledgePoints.find(item => item.id === parentId)
    if (parent) {
      const child = parent.children.find(item => item.id === id)
      if (child) {
        child.mastered = mastered
        // 检查父节点状态
        parent.mastered = parent.children.every(child => child.mastered)
      }
    }
  } else {
    // 更新父知识点和所有子知识点
    const item = enrolledCompetition.knowledgePoints.find(item => item.id === parentId)
    if (item) {
      item.mastered = mastered
      item.children.forEach(child => {
        child.mastered = mastered
      })
    }
  }
  
  return {
    code: 200,
    data: enrolledCompetition.knowledgePoints,
    message: 'success'
  }
})

export default {
  enrolledCompetition,
  checkInData
}
